package com.lengmou.ctrl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.lengmou.entity.Coupons;
import com.lengmou.service.CouponsService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/coupons")
public class CouponsController {
    @Resource
    private CouponsService couponsService;




//搜索获取
    @RequestMapping(value = "/getAll",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String search(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                         @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                         String id,Double cut,Double full,Integer state,String createtime,String usetime,Integer ordersid){
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("cut",cut);
        map.put("full",full);
        map.put("state",state);
        map.put("createtime",createtime);
        map.put("usetime",usetime);
        map.put("ordersid",ordersid);
        PageHelper.startPage(pageNum,pageSize);
        List<Coupons> search = couponsService.search(map);
        JSONObject json = new JSONObject();
        json.put("code",0);
        json.put("data",search);
        json.put("count",couponsService.search(map).size());
        return json.toString();
    }
    @RequestMapping(value = "/jsonCoupons",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String jsonCoupons(){
        JSONObject json = new JSONObject();
        List<Coupons> all = couponsService.getAll();
        json.put("coupons",all);
        return json.toString();
    }


    @RequestMapping(value = "/getCoupons",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String getCoupons(String id){
        JSONObject json = new JSONObject();
        Coupons couponsById = couponsService.getCouponsById(id);
        if(couponsById!=null&&couponsById.getState()!=1){
            json.put("state",1);
            json.put("coupons",couponsById);
        }else{
            json.put("state",0);
        }
        return json.toString();
    }

    @RequestMapping(value = "/setCut",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String setCuts(Coupons coupons){
        JSONObject json = new JSONObject();
       if(coupons!=null){

           if(coupons.getCut()>0){
               int i = couponsService.setCouponsCut(coupons);
               json.put("state",i);
           }else{
               json.put("state",-2);
           }
       }else{
           json.put("state",-1);
       }
        return json.toString();
    }

    @RequestMapping(value = "/setFull",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String setFull(Coupons coupons){
        JSONObject json = new JSONObject();
        if(coupons!=null){
            int i = couponsService.setCouponsFull(coupons);
            json.put("state",i);
        }else{
            json.put("state",-1);
        }
        return json.toString();
    }

    @RequestMapping(value = "/setState",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String setState(Coupons coupons){
        JSONObject json = new JSONObject();
        if(coupons!=null){
            int i = couponsService.setCouponsState(coupons);
            json.put("state",i);
        }else{
            json.put("state",-1);
        }
        return json.toString();
    }

    @RequestMapping(value = "/setcreatetime",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String setcreatetime(Coupons coupons){
        JSONObject json = new JSONObject();
        if(coupons!=null){
            int i = couponsService.setCouponsCreateTime(coupons);
            json.put("state",i);
        }else{
            json.put("state",-1);
        }
        return json.toString();
    }
    @RequestMapping(value = "/setusetime",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String setusetime(Coupons coupons){
        JSONObject json = new JSONObject();
        if(coupons!=null){
            int i = couponsService.setCouponsUseTime(coupons);
            json.put("state",i);
        }else{
            json.put("state",-1);
        }
        return json.toString();
    }
    @RequestMapping(value = "/setOrdersid",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String setOrdersid(Coupons coupons){
        JSONObject json = new JSONObject();
        if(coupons!=null){
            int i = couponsService.setCouponsOrdersId(coupons);
            json.put("state",i);
        }else{
            json.put("state",-1);
        }
        return json.toString();
    }


    @RequestMapping(value = "/addCoupons",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String addCoupons(Double cut,Double full,Integer num){
        JSONObject json = new JSONObject();
        int i = couponsService.addsCoupons(num, cut, full);
        json.put("num",i);
        return json.toString();
    }

    @RequestMapping(value = "/deleteAll",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String deleteAll( @RequestBody List<Coupons> cars){
        JSONObject json = new JSONObject();
        if(cars!=null&&cars.size()>0){
            int i = 0;
            for (Coupons o:cars) {
                i+=couponsService.deleteCoupons(o.getId());
            }
            json.put("state",i);
        }else{
            json.put("state",-1);
        }
        return json.toString();
    }


}
